package com.epower.web.action.epoweruserword;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epoweruserword.EpowerUserWordService;
import com.epower.bo.epoweruserword.impl.EpowerUserWordServiceImpl.WordCountResult;
import com.epower.bo.epowerword.EpowerWordService;
import com.epower.dataobject.EpowerUserDO;
import com.epower.dataobject.EpowerUserWordDO;
import com.epower.dataobject.EpowerWordDO;

@Scope("prototype")
@Component()
public class EpowerUserWordAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/*** 查询对象 ***/
	private EpowerUserWordDO query;

	/*** 对象集合 ***/
	private List<EpowerUserWordDO> epowerUserWordList;

	public List<EpowerWordDO> epowerWordList;

	/*** BO ***/
	private EpowerUserWordService epowerUserWordBO;

	/*** 主键 ***/
	private Integer id;

	/*** 查询对象 ***/
	private EpowerUserWordDO epowerUserWord;

	/*** 当前action所在的namespace ***/
	private String namespace = "epoweruserword";
	/*** 当前action对应的module ***/
	private String moduleName = "EpowerUserWord";

	private int bookid;
	
	private int level;
	
	private int wordid;
	
	private EpowerWordService epowerWordBO;
	private Result result = Result.succResult();

	/***
	 * 用户根据状态获取单词的列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listEpowerUserWord() throws Exception {
		EpowerUserDO user = this.getUser();
		if (user == null) {
			return ERROR;
		}
		// 初始化查询条件
		if (query == null) {
			query = new EpowerUserWordDO();
		}
		query.setUserid(user.getId());
		// 查询列表
		epowerWordList = epowerUserWordBO.getEpowerWordsByOption(query);
		pages = this.getPagesList(query.getToPage(), query.getTotalPage());
		return SUCCESS;
	}

	/**
	 * 获取某个课本的单词总数，熟悉词数，生词数
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAllWordCountByBookid() throws Exception {
		if (bookid < 0) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			result.setResultCode(Result.RESULTCODE_PARAMETER_ILLEGAL);
			resultMap.put("message", "bookid值不能小于0");
			return ERROR;
		}
		EpowerUserDO user = getUser();
		if (user == null) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			result.setResultCode(Result.RESULTCODE_REQUEST_FAILURE);
			resultMap.put("message", "登录后才能操作，请先登录");
			return ERROR;
		}
		int userid = 5;
		int wordCount = epowerWordBO.getWordCountByBookid(bookid);
		WordCountResult countResult = epowerUserWordBO.getFamiliarWordCountAndStrangeWordCount(bookid, userid);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("WordCount", wordCount);
		resultMap.put("FamiliarWordCount", countResult.familiarWordCount);
		resultMap.put("StrangeWordCount", countResult.strangeWordCount);
		result.setResultCode(Result.RESULTCODE_NORMAL);
		result.setResult(resultMap);
		return SUCCESS;
	}

	/***
	 * 更改用户的单词状态
	 * http://localhost:8080/epower-site-webapp-1.0.0/epoweruserword/changeUserWordLevel.action?level=1&&wordid=8
	 * @return
	 * @throws Exception
	 */

	public String changeUserWordLevel() throws Exception {
		JSONObject obj = new JSONObject();
		obj.put("isSuccess", false);
		EpowerUserDO user = this.getUser();
		if (user == null) {
			return ERROR;
		}
		epowerUserWord.setUserid(user.getId());
		if (epowerUserWordBO.updateEpowerUserWordByOption(epowerUserWord)) {
			obj.put("isSuccess", true);
		}
		this.write(obj.toString());
		return SUCCESS;
	}

	/**
	 * 增加熟悉词，生词，或更换状态 需要bookid,wordid,level,userid
	 * 
	 * @return
	 * @throws Exception
	 */
	public String resetUserWordLevel() throws Exception {
		EpowerUserDO user = this.getUser();
		if (user == null) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			result.setResultCode(Result.RESULTCODE_REQUEST_FAILURE);
			resultMap.put("message", "登录后才能操作，请先登录");
			return ERROR;
		}
		result.setResultCode(Result.RESULTCODE_REQUEST_FAILURE);
		if (epowerUserWordBO.queryEpowerUserWordCount(wordid, user.getId(), bookid) > 0) {
			if (epowerUserWordBO.updateEpowerUserWordLevel(bookid, wordid, user.getId(), level)) {
				result.setResultCode(Result.RESULTCODE_NORMAL);
			}
		} else {
			EpowerUserWordDO epowerUserWord = new EpowerUserWordDO();
			epowerUserWord.setBookid(bookid);
			epowerUserWord.setWordid(wordid);
			epowerUserWord.setLevel(level);
			epowerUserWord.setUserid(user.getId());
			epowerUserWord.setStatus(1);
			if (epowerUserWordBO.addEpowerUserWord(epowerUserWord) > 0) {
				result.setResultCode(Result.RESULTCODE_NORMAL);
			}
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("wordid", wordid);
		resultMap.put("bookid", bookid);
		resultMap.put("level", level);
		result.setResult(resultMap);
		return SUCCESS;
	}
	
	public String getMyBooks() throws Exception {//获取用户的词汇
		// EpowerUserDO user = this.getUser();
		// if (user == null) {
		// Map<String, Object> resultMap = new HashMap<String, Object>();
		// result.setResultCode(Result.RESULTCODE_REQUEST_FAILURE);
		// resultMap.put("message", "登录后才能操作，请先登录");
		// return ERROR;
		// }
		
		
		return SUCCESS;
	}
	
	/***
	 * 添加操作
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addEpowerUserWord() throws Exception {
		EpowerUserDO user = this.getUser();

		if (user == null) {
			return ERROR;
		}
		JSONObject obj = new JSONObject();
		obj.put("isSuccess", false);
		epowerUserWord.setUserid(user.getId());
		if (epowerUserWordBO.addEpowerUserWord(epowerUserWord) != null) {
			obj.put("isSuccess", true);
		}

		this.write(obj.toString());
		return SUCCESS;
	}

	/*** setter getter start ***/
	public EpowerUserWordDO getQuery() {
		return query;
	}

	public void setQuery(EpowerUserWordDO query) {
		this.query = query;
	}

	public List<EpowerUserWordDO> getEpowerUserWordList() {
		return epowerUserWordList;
	}

	public void setEpowerUserWordList(List<EpowerUserWordDO> epowerUserWordList) {
		this.epowerUserWordList = epowerUserWordList;
	}

	public EpowerUserWordService getEpowerUserWordBO() {
		return epowerUserWordBO;
	}

	@Resource
	public void setEpowerUserWordBO(EpowerUserWordService epowerUserWordBO) {
		this.epowerUserWordBO = epowerUserWordBO;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public EpowerUserWordDO getEpowerUserWord() {
		return epowerUserWord;
	}

	public void setEpowerUserWord(EpowerUserWordDO epowerUserWord) {
		this.epowerUserWord = epowerUserWord;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public List<EpowerWordDO> getEpowerWordList() {
		return epowerWordList;
	}

	public void setEpowerWordList(List<EpowerWordDO> epowerWordList) {
		this.epowerWordList = epowerWordList;
	}

	public EpowerWordService getEpowerWordBO() {
		return epowerWordBO;
	}

	public void setEpowerWordBO(EpowerWordService epowerWordBO) {
		this.epowerWordBO = epowerWordBO;
	}

	public int getBookid() {
		return bookid;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public void setBookid(int bookid) {
		this.bookid = bookid;
	}

	public int getWordid() {
		return wordid;
	}

	public void setWordid(int wordid) {
		this.wordid = wordid;
	}

	public Result getResult() {
		return result;
	}

	/*** setter getter end ***/
}
